﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PrinterBudi.Persistence;

namespace PrinterBudi.Desktop
{
    public partial class FormPBUser : Form
    {
        private PBUser userNow;

        public FormPBUser()
        {
            InitializeComponent();

            EditMode(false);
        }

        private void EditMode(bool status)
        {
            tbNameUser.Enabled = status;
            tbPasswordUser.Enabled = status;
            tbAddressUser.Enabled = status;
            btnEditOK.Visible = status;
            btnEditCancel.Visible = status;

            btnEditUser.Visible = !status;
            btnAddUser.Visible = !status;
            btnDeleteUser.Visible = !status;
            btnViewUser.Visible = !status;
            btnClose.Visible = !status;

        }

        private void LoadcbPhoneNumberItems()
        {
            List<PBUser> listUser = PBUserModel.ReadAllUser();

            cbPhoneNumberUser.Items.Clear();
            foreach (PBUser user in listUser)
                cbPhoneNumberUser.Items.Add(user.PhoneNumber);

            cbPhoneNumberUser.SelectedIndex = 0;
        }

        private void FormPBUser_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'printerBudiDataSet.PBUser' table. You can move, or remove it, as needed.
            //this.pBUserTableAdapter.Fill(this.printerBudiDataSet.PBUser);
            LoadcbPhoneNumberItems();
        }

        private void cbPhoneNumberUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            userNow = PBUserModel.Read(cbPhoneNumberUser.Text);

            tbNameUser.Text = userNow.Name;
            tbPasswordUser.Text = userNow.Password;
            tbAddressUser.Text = userNow.Address;
        }

        private void btnEditUser_Click(object sender, EventArgs e)
        {
            EditMode(true);
        }


        private void btnEditOK_Click(object sender, EventArgs e)
        {
            userNow.Name = tbNameUser.Text;
            userNow.Address = tbAddressUser.Text;
            userNow.Password = tbPasswordUser.Text;

            PBUserModel.Update(userNow);
            LoadcbPhoneNumberItems();
            EditMode(false);
        }

        private void btnEditCancel_Click(object sender, EventArgs e)
        {
            LoadcbPhoneNumberItems();
            EditMode(false);
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            PBUserModel.Delete(userNow);
            LoadcbPhoneNumberItems();
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            FormAddPBUser frm = new FormAddPBUser();
            frm.ShowDialog();
            LoadcbPhoneNumberItems();
        }

        private void btnViewUser_Click(object sender, EventArgs e)
        {
            FormViewPBUser frm = new FormViewPBUser();

            this.Hide();
            frm.ShowDialog();

            this.Show();
            this.Activate();
            LoadcbPhoneNumberItems();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
